package com.ymatou.iapi.sellerproduct.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.parameter.ProdTplBean;
import com.ymatou.iapi.sellerproduct.service.QueryProductPropertyCall;
import com.ymatou.iapi.sellerproduct.service.SaveProductTemplateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class Ts_QueryProductProperty {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static QueryProductPropertyCall queryProductPropertyCallCall = new QueryProductPropertyCall();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("Get property detail");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryProductPropertyCallCall.getOpurl());
	}
	
	
	@After 
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductProperty_001() {
		Logger.start(true, "Get property detail");
		try{
			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			saveProductTemplateCall.deleteTemplate(sellerId);
			//	模版类型（1，尺码模版 2，买家须知 3，买手服务）
			ProdTplBean sizeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 1);
			ProdTplBean buyerNoticeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 2);
			ProdTplBean sellerServiceTemplate = saveProductTemplateCall.saveTemplate(sellerId, 3);
			
			//get property 
			Map<String, Object> thirdCategoryAndPropertyVal = SellerProductCallService.getThirdCategoryAndPropertyValue();
			
			//发布商品
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl.json");	
			Map<String, Object> updateMap = MapUtil.hashMap();
			updateMap.put("UserID", sellerId);
			updateMap.put("LoginID", sellerName);
			updateMap.put("Product->iThirdCategoryId", thirdCategoryAndPropertyVal.get("ThirdCategory"));
			updateMap.put("Product->PropertyList", (JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			updateMap.put("Product->SizeTemplateId", sizeTemplate.getTemplateId());
			updateMap.put("Product->BuyerNoticeId",buyerNoticeTemplate.getTemplateId());
			updateMap.put("Product->SellerServiceId", sellerServiceTemplate.getTemplateId());	
			JsonBeanHelper.updateJsonBean(addProductBean, updateMap);
			
			String productId = SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
			
			//属性检查
			SellerProductCallService.checkProdProperty(productId,(JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			
			//模板检查
			//check product template
			List<ProdTplBean> prodTplBeanList = Arrays.asList(sizeTemplate,buyerNoticeTemplate,sellerServiceTemplate);
			SellerProductCallService.checkTemplateWithProd(productId,prodTplBeanList);
		
			
			//check without product id 
			JSONObject ret = queryProductPropertyCallCall.callAndGetReturnData(MapUtil.hashMap("ThirdCategoryId",thirdCategoryAndPropertyVal.get("ThirdCategory")));
			JSONArray propertyList = ret.getJSONObject("Data").getJSONArray("PropertyList");
			SellerProductCallService.checkPropertyList(propertyList, Integer.parseInt(thirdCategoryAndPropertyVal.get("ThirdCategory").toString()), null);
			

			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
}
